#!/bin/bash

############################################################################
#
# 远程执行该脚本，请在命令行中直接执行以下命令：
#
#   curl -sSL https://gitee.com/tay3223/biubiubiu/raw/master/k8s/k8s-v1.28.2-centos79-init.sh | /bin/bash
#
#
############################################################################
#
# 先使用701号脚本初始化好centos79的机器
# 在linux裸机上直接执行该脚本，就可以安装好k8s-1.28.2相关的 <容器运行时三大件> + <kube三剑客> + <crictl> + <nerdctl>
# 已经处理过脚本逻辑，可以重复多次运行
# 该安装方法没有抄自任何博客，百分百内容来自于1.28.x的k8s官方文档：https://v1-28.docs.kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/




#***********************
# 修改DNS
#***********************
cat <<\EOF > /etc/resolv.conf
nameserver 223.5.5.5
nameserver 114.114.114.114
EOF




#***********************
# 安装必备组件
#***********************
#安装k8s必备工具（要安装这些依赖是基于这个版本的镜像为基础才需要安装：Rocky-9-GenericCloud-Base.9.4.x86_64.qcow2）
yum install -y conntrack-tools iptables ebtables socat




#***********************
#关闭swap并确保重启后不启用
#***********************
swapoff -a
if grep -q '^[^#].* swap ' /etc/fstab; then
    sed -i '/ swap / s/^/#/' /etc/fstab
fi




#***********************
#加载br_netfilter模块并确保重启后加载
#***********************
if ! lsmod | grep -q br_netfilter; then
    modprobe br_netfilter
fi
if ! grep -q 'br_netfilter' /etc/modules-load.d/br_netfilter.conf; then
    echo 'br_netfilter' > /etc/modules-load.d/br_netfilter.conf
fi




#***********************
# 内核转发、禁用swap、网桥过滤
#***********************
cat <<\EOF > /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
vm.swappiness = 0
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf




#***********************
# 修改hosts文件
#***********************
HOSTS_BLOCK=$(cat << 'EOF'

#由于懒得创建LB代理6443端口，因此这里注册一个域名用来给k8s使用，后期可以把这个dns指向到LB上
192.168.5.229 k8s-master-02.tianye.work

#master节点
192.168.5.221 centos7-k8s1
192.168.5.222 centos7-k8s2
192.168.5.223 centos7-k8s3

#node节点
192.168.5.224 centos7-k8s4
192.168.5.225 centos7-k8s5
192.168.5.226 centos7-k8s6
192.168.5.227 centos7-k8s7
192.168.5.228 centos7-k8s8

EOF
)

# 检查并追加到 /etc/hosts
if ! grep -q "192.168.5.225" /etc/hosts; then
    echo "$HOSTS_BLOCK" >> /etc/hosts
fi




#***********************
# 禁止cloud-init在重启后改写hosts文件
#***********************
#
# 方法一：cloud-init会重写/etc/hosts，因此在cloud-init的模板文件中也添加一下解析配置，防止下次重启后/hosts丢失
#if ! grep -q "192.168.5.235" /etc/cloud/templates/hosts.redhat.tmpl; then
#    echo "$HOSTS_BLOCK" >> /etc/cloud/templates/hosts.redhat.tmpl
#fi

# 方法二：当然要可以选择把 manage_etc_hosts 设置为 False，默认情况下值是True（细节没时间研究，直接选择方法一了）
# 永久关闭 manage_etc_hosts
if grep -q 'manage_etc_hosts' /etc/cloud/cloud.cfg; then
    sed -i 's/manage_etc_hosts: true/manage_etc_hosts: false/' /etc/cloud/cloud.cfg
else
    echo 'manage_etc_hosts: false' >> /etc/cloud/cloud.cfg
fi




######################################################################################################################
######################################################################################################################
######################################################################################################################




#***********************
# 安装containerd
# 官方文档：https://github.com/containerd/containerd/blob/main/docs/getting-started.md
#***********************
mkdir -p ~/bao && cd ~/bao

#这个速度慢，但是省钱（最好的办法是内网新建一个file服务器，明天再搞）
#wget https://file.taycc.com/d/bao/k8s/v1.28.2/containerd-1.7.23-linux-amd64.tar.gz
#这个速度快，但是每个月都要支付下行流量包的费用
wget -O ./containerd-1.7.23-linux-amd64.tar.gz http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/containerd-1.7.23-linux-amd64.tar.gz

mkdir -p ./bin && rm -rf ./bin/*

tar Czxvf /usr/local ./containerd-1.7.23-linux-amd64.tar.gz

mkdir -p /etc/containerd
/usr/local/bin/containerd config default > /etc/containerd/config.toml

#因为下文已经有代理的存在了，所以就不用指定aliyun的镜像地址，也就不会出现版本不一致的问题
sed -i 's|registry.k8s.io/pause:3.8|registry.aliyuncs.com/google_containers/pause:3.9|g' /etc/containerd/config.toml

# 检查配置是否已存在（代理暂时还没有调试通，先用aliyun的源顶着）
#if ! grep -q '\[plugins."io.containerd.grpc.v1.cri".registry.configs."docker.io".proxy\]' /etc/containerd/config.toml; then
#cat <<\EOF >> /etc/containerd/config.toml
#[plugins."io.containerd.grpc.v1.cri".registry.configs."docker.io".proxy]
#  http_proxy = "http://192.168.5.246:7890"
#  https_proxy = "http://192.168.5.246:7890"
#  no_proxy = "localhost,127.0.0.1,.example.com,.tianye.work"
#EOF
#fi

cat <<\EOF > /etc/systemd/system/containerd.service

# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target dbus.service

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target

EOF
systemctl daemon-reload
systemctl enable --now containerd




#***********************
# 安装runc
# 官方文档：https://github.com/containerd/containerd/blob/main/docs/getting-started.md#step-2-installing-runc
#***********************
mkdir -p ~/bao && cd ~/bao
wget -O ./runc-1.1.13.amd64 http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/runc-1.1.13.amd64
install -o root -g root -m 755 runc-1.1.13.amd64 /usr/local/sbin/runc




#***********************
# 安装cni
# 官方文档：https://github.com/containerd/containerd/blob/main/docs/getting-started.md#step-3-installing-cni-plugins
# 包地址：https://github.com/containernetworking/plugins/releases/download/v1.3.0/cni-plugins-linux-amd64-v1.3.0.tgz
#***********************
mkdir -p ~/bao && cd ~/bao
wget -O ./cni-plugins-linux-amd64-v1.5.1.tgz http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/cni-plugins-linux-amd64-v1.5.1.tgz
mkdir -p /opt/cni/bin && rm -rf /opt/cni/bin/*
tar Cxzvf /opt/cni/bin ./cni-plugins-linux-amd64-v1.5.1.tgz

#cat <<\EOF > /etc/profile.d/cni.conf
#export PATH=/opt/cni/bin:$PATH
#EOF
#source /etc/profile.d/cni.conf




#***********************
# 安装nerdctl
# 官方文档：https://github.com/containerd/nerdctl?tab=readme-ov-file#install
#***********************
mkdir -p ~/bao && cd ~/bao
wget -O ./nerdctl-1.7.7-linux-amd64.tar.gz http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/nerdctl-1.7.7-linux-amd64.tar.gz
tar Czxvf /usr/local/bin  ./nerdctl-1.7.7-linux-amd64.tar.gz




######################################################################################################################
######################################################################################################################
######################################################################################################################




##***********************
## 安装crictl
## 官网文档：https://v1-28.docs.kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
## 包地址：https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.28.0/crictl-v1.28.0-linux-amd64.tar.gz
##***********************
#mkdir -p ~/bao && cd ~/bao
#wget -O ./crictl-v1.28.0-linux-amd64.tar.gz http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/crictl-v1.28.0-linux-amd64.tar.gz
#tar zxvf crictl-v1.28.0-linux-amd64.tar.gz -C /usr/local/bin/
#chown root:root /usr/local/bin/crictl
#
#
#
#
##***********************
## 安装kubeadm
## 官网文档：https://v1-28.docs.kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
## 包地址：https://dl.k8s.io/release/v1.28.2/bin/linux/amd64/kubeadm
##***********************
#cd /usr/local/bin
#wget -O ./kubeadm-1.28.2 http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/kubeadm-1.28.2
#mv kubeadm-1.28.2 kubeadm
#chmod +x kubeadm
#
#
#
#
##***********************
## 安装kubelet
## 官网文档：https://v1-28.docs.kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
## 包地址：包地址：https://dl.k8s.io/release/v1.28.2/bin/linux/amd64/kubelet
##***********************
#cd /usr/local/bin
#wget -O ./kubelet-1.28.2 http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/kubelet-1.28.2
#mv kubelet-1.28.2 kubelet
#chmod +x kubelet
#
##文档地址：https://v1-28.docs.kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
##模板地址：https://raw.githubusercontent.com/kubernetes/release/v0.16.2/cmd/krel/templates/latest/kubelet/kubelet.service
#cat << \EOF > /etc/systemd/system/kubelet.service
#[Unit]
#Description=kubelet: The Kubernetes Node Agent
#Documentation=https://kubernetes.io/docs/
#Wants=network-online.target
#After=network-online.target
#
#[Service]
#ExecStart=/usr/bin/kubelet
#Restart=always
#StartLimitInterval=0
#RestartSec=10
#
#[Install]
#WantedBy=multi-user.target
#EOF
#
##文档地址：https://v1-28.docs.kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
##模板地址：https://raw.githubusercontent.com/kubernetes/release/v0.16.2/cmd/krel/templates/latest/kubeadm/10-kubeadm.conf
#mkdir -p /etc/systemd/system/kubelet.service.d
#cat << \EOF > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
## Note: This dropin only works with kubeadm and kubelet v1.11+
#[Service]
#Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
#Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
#
## This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
#EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
#
## This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
## the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
#EnvironmentFile=-/etc/sysconfig/kubelet
#ExecStart=
#ExecStart=/usr/local/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
#EOF
#
#systemctl daemon-reload
#systemctl enable --now kubelet
#
#
#
#
##***********************
## 安装kubectl
## 官网文档：https://v1-28.docs.kubernetes.io/zh-cn/docs/tasks/tools/
## 包地址：https://dl.k8s.io/release/v1.28.2/bin/linux/amd64/kubectl
##***********************
#mkdir -p ~/bao && cd ~/bao
#wget -O ./kubectl-1.28.2 http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/kubectl-1.28.2
#install -o root -g root -m 0755 kubectl-1.28.2 /usr/local/bin/kubectl
#kubectl version --client



#***********************
# 使用yum的形式安装组件
#***********************
#cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
#[kubernetes]
#name=Kubernetes
#baseurl=https://pkgs.k8s.io/core:/stable:/v1.28/rpm/
#enabled=1
#gpgcheck=1
#gpgkey=https://pkgs.k8s.io/core:/stable:/v1.28/rpm/repodata/repomd.xml.key
#exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
#EOF

cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet




######################################################################################################################
######################################################################################################################
######################################################################################################################




cat << EOF

====================================
完成安装：

1.容器运行时：
    - containerd
    - runc
    - cni
    - nerdctl

2.kubeadm相关组件
    - kubelet
    - kubeadm
    - kubectl
    - crictl





相关命令：
    systemctl daemon-reload
    systemctl restart containerd
    systemctl status containerd
    systemctl restart kubelet
    systemctl status kubelet
====================================

EOF




